<?php
include "header.php";
?>

    <div class="container-fluid">
<!--      <div class="row">-->
<!--        <div class="col-sm-3 col-md-2 sidebar">-->
<!--          <ul class="nav nav-sidebar">-->
<!--            <li class="active">-->
<!--                <a href="#">公云<span class="sr-only">(current)</span></a>-->
<!--            </li>-->
<!--            <li><a href="#">熙地港</a></li>-->
<!--            <li><a href="#">望京凯德</a></li>-->
<!--          </ul>-->
<!---->
<!--        </div>-->
        <div class="col-sm-12  col-md-12  main">
          <h1 class="page-header">商场列表
              <button id="btn_cancelCreateMerchant" type="button" class="btn btn-lg btn-warning pull-right" onclick="cancelCreateMerchant()" style="display: none">取消</button>
              <button id="btn_finish" type="button" class="btn btn-lg btn-success pull-right" onclick="finishCreateMerchant()" style="display: none">保存</button>
              <button id="btn_modify" type="button" class="btn btn-lg btn-success pull-right" onclick="finishModifyMerchant()" style="display: none">修改</button>
              <?php if($_COOKIE['userrole']=="1") {
                  ?>
                  <button id="btn_create" type="button" class="btn btn-lg btn-success pull-right"
                          onclick="showCreateMerchantDiv()">创建
                  </button>
                  <?php
                    }
              ?>

          </h1>

            <div style="padding: 100px 100px 10px;display: none" id="div_createMerchant">
                <form class="bs-example bs-example-form" role="form">
                    <div class="input-group">
                        <span class="input-group-addon">所属服务器</span>
                        <select id="select_server" class="cls_select2 form-control" name="server" >

                        </select>
                    </div>
                    <br />
                    <div class="input-group">
                        <span class="input-group-addon">商场名称</span>
                        <input id="input_name" type="text" class="form-control" placeholder="请输入商场名称" onblur="checkWidgetEcho(this.id,'merchant','name',this.value,current_modifymerchant_id,'商场名')">
                    </div>
                    <br>

                    <div class="input-group">
                        <span class="input-group-addon">位置信息</span>

                        <select id="select_location1" class="cls_select2_50 form-control" name="location1" onchange="getLocation(2,this.value)">

                        </select>
                        <select id="select_location2" class="cls_select2_50 form-control" name="location2" onchange="getLocation(3,this.value)">

                        </select>
                        <select id="select_location3" class="cls_select2_50 form-control" name="location3" onchange="getLocation(4,this.value)">

                        </select>
                        <select id="select_location4" class="cls_select2_50 form-control" name="location4">

                        </select>
                    </div>
                    <br>

                    <div class="input-group">
                        <span class="input-group-addon">地址</span>
                        <input id="input_address" type="text" class="form-control" placeholder="请输入商场详细地址">
                    </div>
                    <br>

                    <div class="input-group">
                        <span class="input-group-addon">楼层信息</span>

                        <select id="select_floor" class="cls_select2_mt form-control" name="floor">

                        </select>
                    </div>
                    <br>
                    <div class="input-group">
                        <span class="input-group-addon">购物中心类别</span>

                        <select id="select_merchanttype" class="cls_select2 form-control" name="merchanttype">

                        </select>

                    </div>
                    <br>

                    <div class="input-group">
                        <span class="input-group-addon">购物中心体量</span>
                        <input id="input_area" type="text" class="form-control" placeholder="请输入面积(m²)">
                    </div>
                    <br>

                    <div class="input-group">
                        <span class="input-group-addon">可操作的角色</span>
                        <select id="select_role" class="cls_select2_mt form-control" name="role">

                        </select>
                    </div>
                    <br>

                </form>
            </div>

          <div class="table-responsive">
            <table class="table table-striped " id="merchantTable" >

            </table>
          </div>
        </div>
      </div>
    </div>



    <script>
        var tableurl="api.php?method=getMerchantTableByUserid";
        var $table = $('#merchantTable');
        $table.bootstrapTable({
            //url: "merchantTable.json",
            url:tableurl,
            dataType: "json",
            pagination: true, //分页
            singleSelect: false,
            search: true, //显示搜索框
            pageSize:10,
            columns: [
            {
                title: 'ID',
                field: 'id',
                align: 'left',
                valign: 'middle',
                sortable : true
            },
            {
                title: '唯一标识',
                field: 'uuid',
                align: 'left',
                valign: 'middle',
                sortable : true
            },
            {
                title: '商场名',
                field: 'name',
                align: 'left',
                valign: 'middle',
                sortable : true
            },
            {
                title: '地址',
                field: 'address',
                align: 'left',
                valign: 'middle',
                sortable : true
            },
            {
                title: '服务器',
                field: 'servername',
                align: 'left',
                valign: 'middle',
                sortable : true
            },
            {
                title: '类型',
                field: 'merchanttypename',
                align: 'left',
                valign: 'middle',
                sortable : true
            },
            {
                title: '操作',
                field: 'id',
                align: 'left',
                formatter:function(value,row,index){
                    s=e=e0=d="";
                    if(row.M=="1")
                    {
                        e0='<a href="#" mce_href="#" onclick="editM(\''+ row.id + '\',\''+row.uuid+'\',\''+row.server_id+'\',\''+row.name+'\',\''+row.location_info+'\',\''+row.merchant_type+'\',\''+row.area+'\',\''+row.address+'\')">编辑 </a>'

                    }
                    e = '<a href="#" mce_href="#" onclick="manageStore(\''+ row.uuid + '\')">管理店铺 </a> ';

                     if(row.D=="1")
                        d = '<a href="#" mce_href="#" onclick="delM(\''+ row.uuid +'\',\''+row.id+'\')">删除</a> ';

                    return s+e0+e+d;
                }
            }
        ]
        });


        $('.cls_select2').select2({width:"100%"});
        $('.cls_select2_50').select2({width:"50%"});
        $('.cls_select2_mt').select2({width:"100%",multiple: true});
        getServers();
        getLocation(1,0);
        getFloors();
        getMerchanttype();
        getRoles();


        current_modifymerchant_id=0;
        current_modifymerchant_uuid=0;
        function editM(id,uuid,server_id,name,location_info,merchant_type,area,address)
        {
            //leak floor_info, ,roles
            current_modifymerchant_id = id;
            current_modifymerchant_uuid = uuid;
            $("#select_server").val(server_id).trigger("change");
            $("#input_name").val(name);
            $("#input_address").val(address);
            arrlocations=location_info.split(',');
            for(i=1;i<arrlocations.length-1;i++)
            {
                if(i==1)
                {
                    $("#select_location1").val(arrlocations[1]).trigger("change");
                }
                else if(i==2)
                {
                    $("#select_location2").val(arrlocations[2]).trigger("change");
                }
                else if(i==3)
                {
                    $("#select_location3").val(arrlocations[3]).trigger("change");
                }
                else if(i==4)
                {
                    $("#select_location4").val(arrlocations[4]).trigger("change");
                }
            }
            $("#select_merchanttype").val(merchant_type).trigger("change");
            $("#input_area").val(area);

            getMerchantFloorInfo(uuid)
            getMerchantInRoles(id)

            showModifyMerchantDiv();
        }

        function delM(merchantuuid,merchant_id)
        {
            bootbox.prompt("<font color='red'>确定要删除此商场吗？</font>（此操作不可撤销！如需删除请在下方输入“确认删除”）",function (result) {
                if(result=="确认删除")
                {
                    $.ajax({
                        url:'api.php?method=delMerchant',
                        type:'POST', //GET
                        async:true,    //或false,是否异步
                        data:{
                            merchant_uuid:merchantuuid,
                            merchant_id:merchant_id
                        },
                        timeout:5000,    //超时时间
                        dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                        beforeSend:function(xhr){
                            console.log(xhr)
                            console.log('发送前')
                        },
                        success:function(data,textStatus,jqXHR){
                            //alert(data.name);
                            if(data.return_code=="0")
                            {

                                bootbox.alert("删除成功",function () {

                                    $("#merchantTable").bootstrapTable('refresh',{'url':tableurl});
                                });
                            }
                            else
                            {
                                if(data.return_code=="-2")
                                    bootbox.alert("删除商场前，请清空商场下的店铺");
                            }



                        },
                        error:function(xhr,textStatus){
                            console.log('错误')
                            console.log(xhr)
                            console.log(textStatus)
                        },
                        complete:function(){
                            console.log('结束')
                        }
                    });
                }
            })
        }

        function getMerchantInRoles(mid)
        {
            $.ajax({
                url:'api.php?method=getMerchantInRoles',
                type:'POST', //GET
                async:false,    //或false,是否异步
                data:{
                    merchant_id:mid
                },
                timeout:5000,    //超时时间
                dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                beforeSend:function(xhr){
                    console.log(xhr)
                    console.log('发送前')
                },
                success:function(data,textStatus,jqXHR){
                    //alert(data.name);
                    roles=new Array();
                    for(o in data)
                    {
                        if(data[o].id==undefined)continue;
                        roles.push(data[o].id);


                    }
                    $("#select_role").val(roles).trigger("change");





                },
                error:function(xhr,textStatus){
                    console.log('错误')
                    console.log(xhr)
                    console.log(textStatus)
                },
                complete:function(){
                    console.log('结束')
                }
            });
        }

        function getMerchantFloorInfo(muuid)
        {
            $.ajax({
                url:'api.php?method=getMerchantFloorInfo',
                type:'POST', //GET
                async:false,    //或false,是否异步
                data:{
                    merchant_uuid:muuid
                },
                timeout:5000,    //超时时间
                dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                beforeSend:function(xhr){
                    console.log(xhr)
                    console.log('发送前')
                },
                success:function(data,textStatus,jqXHR){
                    //alert(data.name);
                    floors=new Array();
                    for(o in data)
                    {
                        if(data[o].id==undefined)continue;
                        floors.push(data[o].id);


                    }
                    $("#select_floor").val(floors).trigger("change");





                },
                error:function(xhr,textStatus){
                    console.log('错误')
                    console.log(xhr)
                    console.log(textStatus)
                },
                complete:function(){
                    console.log('结束')
                }
            });
        }

        function manageStore(uuid)
        {
            location.href="showMerchantSStoreAndIDMS.php?merchant_uuid="+uuid;
        }

        function showCreateMerchantDiv()
        {
            $("#div_createMerchant").show()
            $("#btn_cancelCreateMerchant").show();
            $("#btn_finish").show();
            $("#btn_create").hide();
            current_modifymerchant_id=0;
            $(".table-responsive").hide();
        }

        function cancelCreateMerchant()
        {
            $("#div_createMerchant").hide();
            $("#btn_cancelCreateMerchant").hide();
            $("#btn_finish").hide();
            $("#btn_modify").hide();
            $("#btn_create").show();

            $("#input_name").val("");
            $("#input_address").val("");
            $("#input_area").val("");

            $("#select_floor").val("").trigger("change");
            $("#select_role").val("").trigger("change");
            $("#select_merchanttype").val("").trigger("change");
            $("#select_server").val("").trigger("change");
            //$("#select").val("").trigger("change");
            $(".table-responsive").show();

        }

        function showModifyMerchantDiv()
        {
            $("#div_createMerchant").show();
            $("#btn_cancelCreateMerchant").show();
            $("#btn_modify").show();
            $("#btn_finish").hide();
            $("#btn_create").hide();
            $(".table-responsive").hide();




        }

        function checkMerchantInput()
        {
            name = $("#input_name").val();

            floor = $("#select_floor").val();

            area = $("#input_area").val();

            server=$("#select_server").val();

            merchanttype=$("#select_merchanttype").val();

            roles=$("#select_role").val();

            location1=$("#select_location1").val();
            location2=$("#select_location2").val();
            location3=$("#select_location3").val();
            location4=$("#select_location4").val();

            address=$("#input_address").val();

            location_info=","+location1+",";
            if(location2!=null)
            {
                location_info+=location2+",";
            }
            if(location3!=null)
            {
                location_info+=location3+",";
            }
            if(location4!=null)
            {
                location_info+=location4+",";
            }

            if(name=="")
            {
                alert("商场名称不能为空");
                return false;
            }
            else if(address == "")
            {
                bootbox.alert("地址不能为空",function () {

                })
                return false;
            }
            else if(floor=="")
            {
                bootbox.alert("楼层信息不能为空",function () {

                })
                return false;
            }
            else if(area=="")
            {
                alert("面积不能为空");
                return false;
            }
            else if(roles=="")
            {
                bootbox.alert("操作权限不能为空",function () {

                })
                return false;
            }
            //alert(industrydetail);
            return true;
        }

        function finishModifyMerchant()
        {
            bcheck=checkMerchantInput();
            if(!bcheck)return;
            bootbox.confirm("确定修改此商场的信息吗？",function (result) {

                if(result)
                    $.ajax({
                        url:'api.php?method=modifyMerchant',
                        type:'POST', //GET
                        async:true,    //或false,是否异步
                        data:{
                            server:server,
                            mid:current_modifymerchant_id,
                            muuid:current_modifymerchant_uuid,
                            location_info:location_info,
                            address:address,
                            name:name,
                            floor:floor,
                            area:area,
                            merchanttype:merchanttype,
                            roles:roles
                        },
                        timeout:5000,    //超时时间
                        dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                        beforeSend:function(xhr){
                            console.log(xhr)
                            console.log('发送前')
                        },
                        success:function(data,textStatus,jqXHR){
                            //alert(data.name);
                            if(data.return_code=="0")
                            {
                                bootbox.alert("修改成功",function () {
                                    cancelCreateMerchant();
                                    $("#merchantTable").bootstrapTable('refresh',{'url':tableurl});
                                });
                            }
                            else
                            {
                                alert(data.return_msg);
                            }



                        },
                        error:function(xhr,textStatus){
                            console.log('错误')
                            console.log(xhr)
                            console.log(textStatus)
                        },
                        complete:function(){
                            console.log('结束')
                        }
                    });
            })

        }

        function finishCreateMerchant()
        {
            bcheck=checkMerchantInput();
            if(!bcheck)return;

            //return;

            $.ajax({
                url:'api.php?method=addMerchant',
                type:'POST', //GET
                async:true,    //或false,是否异步
                data:{
                    server:server,
                    location_info:location_info,
                    address:address,
                    name:name,
                    floor:floor,
                    area:area,
                    merchanttype:merchanttype,
                    roles:roles
                },
                timeout:5000,    //超时时间
                dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                beforeSend:function(xhr){
                    console.log(xhr)
                    console.log('发送前')
                },
                success:function(data,textStatus,jqXHR){
                    //alert(data.name);
                    if(data.return_code=="0")
                    {
                        bootbox.alert("创建成功",function () {
                            cancelCreateMerchant();
                            $("#merchantTable").bootstrapTable('refresh',{'url':tableurl});
                        });
                    }
                    else
                    {
                        if(data.return_code=="-3")
                        {
                            bootbox.alert("商场名称重复，请重新输入！");
                            return;
                        }

                    }



                },
                error:function(xhr,textStatus){
                    console.log('错误')
                    console.log(xhr)
                    console.log(textStatus)
                },
                complete:function(){
                    console.log('结束')
                }
            });

        }

        function getRoles()
        {


            $.ajax({
                url:'api.php?method=getRoles',
                type:'POST', //GET
                async:true,    //或false,是否异步
                timeout:5000,    //超时时间
                dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                beforeSend:function(xhr){
                    console.log(xhr)
                    console.log('发送前')
                },
                success:function(data,textStatus,jqXHR){
                    //alert(data.name);
                    for(o in data)
                    {
                        if(data[o].id==undefined)continue;
                        $("#select_role").append("<option value='" + data[o].id + "'>&nbsp;" + data[o].name + "</option>");

                    }



                },
                error:function(xhr,textStatus){
                    console.log('错误')
                    console.log(xhr)
                    console.log(textStatus)
                },
                complete:function(){
                    console.log('结束')
                }
            });




        }

        function getServers()
        {


            $.ajax({
                url:'api.php?method=getServers',
                type:'POST', //GET
                async:true,    //或false,是否异步
                timeout:5000,    //超时时间
                dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                beforeSend:function(xhr){
                    console.log(xhr)
                    console.log('发送前')
                },
                success:function(data,textStatus,jqXHR){
                    //alert(data.name);
                    for(o in data)
                    {
                        if(data[o].id==undefined)continue;
                        $("#select_server").append("<option value='" + data[o].id + "'>&nbsp;" + data[o].name + "</option>");

                    }



                },
                error:function(xhr,textStatus){
                    console.log('错误')
                    console.log(xhr)
                    console.log(textStatus)
                },
                complete:function(){
                    console.log('结束')
                }
            });




        }

        function getMerchanttype()
        {


            $.ajax({
                url:'api.php?method=getMerchanttype',
                type:'POST', //GET
                async:true,    //或false,是否异步
                timeout:5000,    //超时时间
                dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                beforeSend:function(xhr){
                    console.log(xhr)
                    console.log('发送前')
                },
                success:function(data,textStatus,jqXHR){
                    //alert(data.name);
                    for(o in data)
                    {
                        if(data[o].id==undefined)continue;
                        $("#select_merchanttype").append("<option value='" + data[o].id + "'>&nbsp;" + data[o].name + "</option>");

                    }



                },
                error:function(xhr,textStatus){
                    console.log('错误')
                    console.log(xhr)
                    console.log(textStatus)
                },
                complete:function(){
                    console.log('结束')
                }
            });




        }

        function getLocation(level,pid)
        {


            $.ajax({
                url:'api.php?method=getLocation',
                type:'POST', //GET
                data:{
                    level:level,
                    pid:pid
                },
                async:false,    //或false,是否异步
                timeout:5000,    //超时时间
                dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                beforeSend:function(xhr){
                    console.log(xhr)
                    console.log('发送前')
                },
                success:function(data,textStatus,jqXHR){
                    //alert(data.name);
                    $("#select_location"+level).empty();
                    if(data.length!=0)
                    {
                        for(o in data)
                        {
                            if(data[o].id==undefined)continue;
                            $("#select_location"+level).append("<option value='" + data[o].id + "'>&nbsp;" + data[o].name + "</option>");

                        }

                        getLocation((level+1),$("#select_location"+level).val());
                    }




                },
                error:function(xhr,textStatus){
                    console.log('错误')
                    console.log(xhr)
                    console.log(textStatus)
                },
                complete:function(){
                    console.log('结束')
                }
            });




        }

        function getFloors()
        {
            $.ajax({
                url:'api.php?method=getFloors',
                type:'POST', //GET
                async:false,    //或false,是否异步
                timeout:5000,    //超时时间
                dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                beforeSend:function(xhr){
                    console.log(xhr)
                    console.log('发送前')
                },
                success:function(data,textStatus,jqXHR){
                    //alert(data.name);
                    $("#select_floor").empty();
                    for(o in data)
                    {
                        if(data[o].id==undefined)continue;
                        $("#select_floor").append("<option value='" + data[o].id + "'>&nbsp;" + data[o].name + "</option>");

                    }



                },
                error:function(xhr,textStatus){
                    console.log('错误')
                    console.log(xhr)
                    console.log(textStatus)
                },
                complete:function(){
                    console.log('结束')
                }
            });
        }
    </script>
  </body>
</html>
